package com.xiaoxx.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxx.model.dto.sys.SysMsgPublishDto;
import com.xiaoxx.model.entity.sys.SysMsg;
import com.xiaoxx.model.entity.sys.SysMsgPublish;
import com.xiaoxx.mapper.SysMsgPublishMapper;
import com.xiaoxx.model.vo.sys.SysMsgPublishListVo;
import com.xiaoxx.model.vo.sys.SysMsgPublishQuery;
import com.xiaoxx.model.vo.sys.SysMsgPublishVo;
import com.xiaoxx.service.SysMsgPublishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxx.service.SysMsgService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author HX
 * @since 2023-08-03
 */
@Service
public class SysMsgPublishServiceImpl extends ServiceImpl<SysMsgPublishMapper, SysMsgPublish> implements SysMsgPublishService {
    @Resource
    private SysMsgService msgService;

    @Override
    public IPage<SysMsgPublishListVo> listMsgPublish(SysMsgPublishQuery query) {
        IPage<SysMsgPublishListVo> voIPage = baseMapper.listMsgPublish(new Page<SysMsgPublish>(query.getCurrent(), query.getSize()), query);
        return voIPage;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addOrUpd(SysMsgPublishDto dto) {
        if (StringUtils.hasLength(dto.getId())) {
            //修改
            updateById(dto.convert(SysMsgPublish.class));
        } else {
            boolean save = save(dto.convert(SysMsgPublish.class));
            if (save) {
                List<SysMsg> collect = dto.getUserIds().stream().map(i -> {
                    SysMsg sysMsg = new SysMsg();
                    sysMsg.setUserId(i);
                    sysMsg.setTitile(dto.getTitile());
                    sysMsg.setContent(dto.getContent());
                    sysMsg.setMsgType(dto.getMsgType());
                    sysMsg.setUserType(dto.getUserType());
                    return sysMsg;
                }).collect(Collectors.toList());
                if (!collect.isEmpty()) {
                    msgService.saveBatch(collect);
                }
            }
        }
        return true;
    }

    @Override
    public Boolean sendAgain(String id, List<String> userIds) {
        SysMsgPublish sysMsgPublish = getById(id);
        if (sysMsgPublish != null) {
            List<SysMsg> collect = userIds.stream().map(i -> {
                SysMsg sysMsg = new SysMsg();
                sysMsg.setUserId(i);
                sysMsg.setTitile(sysMsgPublish.getTitile());
                sysMsg.setContent(sysMsgPublish.getContent());
                sysMsg.setMsgType(sysMsgPublish.getMsgType());
                sysMsg.setUserType(sysMsgPublish.getUserType());
                return sysMsg;
            }).collect(Collectors.toList());
            if (!collect.isEmpty()) {
                msgService.saveBatch(collect);
            }
        }
        return true;
    }
}
